import scrapy

from ScrapyObject.spiders.utils.url_utils import *

''''
scrapy crawl ltak -o ltak.json
https://338av11.net/main.html
'''


class LtakSpider(scrapy.Spider):
    name = "ltak"
    # 前缀
    prefix = 'https://'
    # 中缀
    website = '338av11'
    # 后缀
    suffix = '.net'
    allowed_domains = [website + '.net']
    start_urls = [prefix + website + suffix + '/main.html']

    def __init__(self):
        self.i = 0

    def parse(self, response):
        content = get_data(response)
        video_url = get_video_url_one(content)
        if len(video_url):
            self.i = self.i + 1
            yield get_video_item(id=self.i, tags='', url=response.url, name='', pUrl='', vUrl=format_url_one(video_url[0]))
        else:
            p_url = response.xpath("//div[@class='vod-item']//a//div//img/@ src").extract()
            names = response.xpath("//a[@class='vod-title']/text()").extract()
            urls = response.xpath("//a[@class='vod-title']/@ href").extract()
            tag_list = response.xpath('/html/head/title/text()').extract()
            if len(p_url) and len(names) and len(urls) and len(tag_list):
                if len(p_url) == len(names) and len(names) == len(urls):
                    index = tag_list[0].find('-')
                    if index != -1:
                        for i in range(len(p_url)):
                            self.i = self.i + 1
                            yield get_video_item(id=self.i, tags=tag_list[0][:index], url=split_joint(self.prefix + self.website + self.suffix, urls[i]), name=names[i], pUrl=p_url[i], vUrl='')
        url_list = get_url(content)
        for url in url_list:
            if url.startswith('/') and url.endswith('.html'):
                yield scrapy.Request(split_joint(self.prefix + self.website + self.suffix, url), callback=self.parse)
